﻿using LibBinary.DataSource;
using LibBinary.FileData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibBinary.FileDeclaration.Binary
{
    public class ConstantDeclaration<T> : IFileDeclaration
        where T : struct, IEquatable<T>
    {
        private readonly T value;

        public ConstantDeclaration(T value)
        {
            this.value = value;
        }

        public AbstractFileData Read(BinaryFileSource src, AbstractFileData parentNode)
        {
            var read = src.Read<T>();
            if (!read.Equals(value))
            {
                throw new ReadingFormatException("Constant data mismatch.");
            }
            return new EmptyData(parentNode);
        }

        public void Write(BinaryFileSource src, AbstractFileData data)
        {
            src.Write<T>(value);
        }
    }
}
